package com.mall.controller;

import com.mall.template.OssTemplate;
import com.mall.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/aliyun/oss")
public class AliyunOssController {

    @Autowired
    private OssTemplate ossTemplate;

    @PostMapping("/upload")
    public Result<Map<String,String>> upload(MultipartFile photoFile) {
        String url = null;
        try {
            String filename = photoFile.getOriginalFilename();
            InputStream inputStream = photoFile.getInputStream();
            url = ossTemplate.upload(filename, inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,String> result = new HashMap<>();
        result.put("url",url);
        return Result.success(result,"操作成功");
    }

}
